package Client;

import Server.Database.Database;
import static Server.Server.DATABASE_PASSWORD;
import static Server.Server.DATABASE_URL;
import static Server.Server.DATABASE_USERNAME;
import static Server.Server.PORT;
import java.awt.HeadlessException;
import java.io.IOException;

import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * Chat client.
 * <p>
 * Todo list:
 * <ul>
 * <li>Administrator interface
 * </ul>
 * 
 * @author P�K
 * @version 3.2
 */

public class Client extends Thread{

	private static final String SERVER_NAME = "127.0.0.1";
	private static final int SERVER_PORT = 12345;
	private static final int PASSWORD_LENGTH = 10;
	private static final int LOGIN_TIME_OUT = 1000;
	private static String username = "";
	private static String password = "";
	private static ClientSocket clientSocket;
        public boolean blocked;
        public boolean admin;

	public Client(){
       // public void run() {

		clientSocket = new ClientSocket(this);
		clientSocket.setTimeout(LOGIN_TIME_OUT);

		boolean connected = false, closeClient = false;

		while (!connected && !closeClient) {

			/*username = inputDialog("Username: ", "input");
			password = inputDialog("Password: ", "password");*/
                        JTextField usernameField = new JTextField();
                        JTextField passwordField = new JPasswordField();
                        Object[] message = {
                            "Darbuotojo vardas:", usernameField,
                            "Slaptažodis:", passwordField
                        };
                                               
                        int option = JOptionPane.showConfirmDialog(null, message, "Darbuotojo prisijungimas", JOptionPane.OK_CANCEL_OPTION);
                        username = usernameField.getText();
                        password = passwordField.getText();
                        //System.out.println(username +", "+ password);
			if (!(username.isEmpty() || password.isEmpty())) {
				try {   
                                        blocked = false;
                                        admin = false;
					clientSocket.connect(SERVER_NAME, SERVER_PORT);
					clientSocket.logIn(username, password);
                                        if (!blocked) {
                                            connected = true;
                                            clientSocket.setGUI(new GUI(this, clientSocket));
                                        }
                                        else {
                                            closeClient = JOptionPane.showConfirmDialog(null,
							"Sorry, but you are not allowed to access system anymore! Good luck! Retry?",
							"Blocked", 0) != 0 ? true : false;
                                        }
				} catch (IOException ioe) {
					ioe.printStackTrace();
					JOptionPane
							.showMessageDialog(
									null,
									"Could not connect to server. "
											+ "Please check SERVER_NAME and SERVER_PORT.");
					closeClient = true;
				} catch (Exception e) {
                                        e.printStackTrace();
					System.out.println(e.getMessage());
					closeClient = JOptionPane.showConfirmDialog(null,
							"Username ("+username+") and/or "
									+ "password ("+password+") are incorrect. Try again?",
							"Retry", 0) != 0 ? true : false;
				}
			} else
				closeClient = JOptionPane.showConfirmDialog(null,
						"Username and/or "
								+ "password are incorrect. Try again?",
						"Retry", 0) != 0 ? true : false;
		}

		if (closeClient)
			System.exit(1);
		System.out.println("SUCCESS!");
		clientSocket.userIsLoggedIn();

	}

	public void close() {

		System.exit(0);

	}
	
	public void disconnect(){
		
		JOptionPane.showConfirmDialog(null, "Nutruko rysys su serveriu.", "Klaida",
				JOptionPane.CLOSED_OPTION);
		System.exit(1);
		
	}
        
	public static void main(String[] args) {
            
		new Client();

	}

	/**
	 * Displays an Input Dialog. Handles HeadlessException.
	 * 
	 * @param title
	 *            - dialog box title
	 * @param type
	 *            - set to "input" or "password"
	 * @return the user input or an empty string if nothing was inputed
	 */
	/*private static String inputDialog(String title, String type) {

		String result = "";

		try {
			switch (type) {
			case "input": {
				result = JOptionPane.showInputDialog(null, null, title,
						JOptionPane.QUESTION_MESSAGE);
			}
				break;
			case "password": {
				JPasswordField passwordField = new JPasswordField(
						PASSWORD_LENGTH);
				if (JOptionPane.showConfirmDialog(null, passwordField, title,
						JOptionPane.OK_CANCEL_OPTION) == 0)
					result = new String(passwordField.getPassword());
			}
				break;
			default: {
				System.out.println("TYPE DOES NOTEXIST");
			}
				break;
			}
		} catch (HeadlessException e) {
			e.printStackTrace();
		}

		if (result == null)
			result = "";

		return result;
	}*/

}